import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _isUndefined from "lodash/isUndefined";
import _isEmpty from "lodash/isEmpty";

var _dec, _dec2, _dec3, _dec4, _dec5, _class, _class2;

/**
 * 租户菜单管理
 * @since 2019-12-2
 */
import React from 'react';
import { connect } from 'dva';
import { Bind } from 'lodash-decorators';
import { Content, Header } from 'components/Page';
import intl from 'utils/intl';
import formatterCollections from 'utils/intl/formatterCollections';
import { filterNullValueObject } from 'utils/utils'; // import Editor from './Editor';

import List from "./List";
import SearchForm from "./SearchForm"; // import PermissionSet from './PermissionSet';

import styles from "./index.less?modules";
var TenantMenuManage = (_dec = connect(function (_ref) {
  var tenantMenuManage = _ref.tenantMenuManage,
      loading = _ref.loading;
  return {
    tenantMenuManage: tenantMenuManage,
    tenantLoading: loading.effects['tenantMenuManage/queryTenant']
  };
}), _dec2 = formatterCollections({
  code: ['ciam.tenantMenu']
}), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec(_class = _dec2(_class = (_class2 = /*#__PURE__*/function (_React$Component) {
  _inherits(TenantMenuManage, _React$Component);

  var _super = _createSuper(TenantMenuManage);

  function TenantMenuManage() {
    var _this;

    _classCallCheck(this, TenantMenuManage);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.tenantForm = void 0;
    _this.searchForm = React.createRef();
    return _this;
  }

  _createClass(TenantMenuManage, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var _this$props = this.props,
          _this$props$location$ = _this$props.location.state;
      _this$props$location$ = _this$props$location$ === void 0 ? {} : _this$props$location$;
      var _back = _this$props$location$._back,
          dispatch = _this$props.dispatch;
      dispatch({
        type: 'tenantMenuManage/initCustomizeList'
      });

      if (_back !== -1) {
        this.handleSearchTenant();
      }
    }
  }, {
    key: "componentDidUpdate",
    value: function componentDidUpdate(prevProps) {
      var state = this.props.location.state;
      var prevState = prevProps.location.state;

      if (state !== prevState) {
        this.updateFormAndSearch();
      }
    } // componentWillUnmount() {
    //   const { history } = this.props;
    //   history.push({
    //     state: {
    //       _back: 0,
    //     },
    //   });
    // }

    /**
     * 检查 location.search 是否发生变化, 发生变化后 设置查询表单+查询
     */

  }, {
    key: "updateFormAndSearch",
    value: function updateFormAndSearch() {
      var state = this.props.location.state;

      if (_isEmpty(state)) {
        this.handleSearchTenant();
      } else if (state.tenantName) {
        // TODO: 租户维护这边 暂时只支持 tenantName 更改
        if (this.searchForm.current) {
          this.searchForm.current.props.form.setFieldsValue({
            tenantName: state.tenantName
          });
        }

        this.handleSearchTenant();
      }
    }
    /**
     * 按条件查询
     */

  }, {
    key: "handleSearchTenant",
    value: function handleSearchTenant() {
      var payload = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var dispatch = this.props.dispatch;
      var form = this.searchForm.current && this.searchForm.current.props && this.searchForm.current.props.form;
      var filterValues = _isUndefined(form) ? {} : filterNullValueObject(form.getFieldsValue());
      dispatch({
        type: 'tenantMenuManage/queryTenant',
        payload: _objectSpread({
          page: _isEmpty(payload) ? {} : payload
        }, filterValues)
      });
    }
    /**
     * 跳转到详情页
     * @param {object} recrod
     */

  }, {
    key: "handleToDetails",
    value: function handleToDetails(recrod) {
      var history = this.props.history;
      history.push("/ciam/tenant-menu-manage/detail/".concat(recrod.tenantId));
    }
  }, {
    key: "render",
    value: function render() {
      var _this$props2 = this.props,
          dispatch = _this$props2.dispatch,
          _this$props2$tenantMe = _this$props2.tenantMenuManage,
          tenantMenuManage = _this$props2$tenantMe === void 0 ? {} : _this$props2$tenantMe,
          path = _this$props2.match.path,
          tenantLoading = _this$props2.tenantLoading;
      var _tenantMenuManage$ten = tenantMenuManage.tenantData,
          tenantData = _tenantMenuManage$ten === void 0 ? {} : _tenantMenuManage$ten,
          pagination = tenantMenuManage.pagination,
          _tenantMenuManage$men = tenantMenuManage.menuTypeList,
          menuTypeList = _tenantMenuManage$men === void 0 ? [] : _tenantMenuManage$men,
          _tenantMenuManage$cus = tenantMenuManage.customizeList,
          customizeList = _tenantMenuManage$cus === void 0 ? [] : _tenantMenuManage$cus;
      var _tenantData$content = tenantData.content,
          content = _tenantData$content === void 0 ? [] : _tenantData$content;
      var listProps = {
        path: path,
        content: content,
        dispatch: dispatch,
        menuTypeList: menuTypeList,
        tenantLoading: tenantLoading,
        tenantPagination: pagination,
        onTenantPageChange: this.handleSearchTenant,
        onHandleToDetails: this.handleToDetails
      };
      var filterProps = {
        onSearch: this.handleSearchTenant,
        wrappedComponentRef: this.searchForm,
        customizeList: customizeList
      };
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Header, {
        title: intl.get('ciam.tenantMenu.view.message.title').d('租户菜单管理')
      }), /*#__PURE__*/React.createElement(Content, null, /*#__PURE__*/React.createElement("div", {
        className: "table-list-search"
      }, /*#__PURE__*/React.createElement(SearchForm, filterProps)), /*#__PURE__*/React.createElement("div", {
        className: styles['ciam-tenant-menu-table']
      }, /*#__PURE__*/React.createElement(List, listProps))));
    }
  }]);

  return TenantMenuManage;
}(React.Component), (_applyDecoratedDescriptor(_class2.prototype, "updateFormAndSearch", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "updateFormAndSearch"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleSearchTenant", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSearchTenant"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleToDetails", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "handleToDetails"), _class2.prototype)), _class2)) || _class) || _class);
export { TenantMenuManage as default };